package jwei.apps.json;

import java.util.ArrayList;
import java.util.List;

import jwei.apps.objects.Product;
import jwei.apps.objects.Style;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 
 * @author Jason Wei
 * 
 */
public class JsonToProduct {

    public static Product convertToProduct(JSONObject j) {
        try {
            String id = j.getString("productId");
            if (id == null || id.equals("")) { return null; }

            String productName = j.getString("productName");
            if (productName == null || productName.equals("")) { return null; }

            String productUrl = "";
            if (j.has("defaultProductUrl")) {
                productUrl = j.getString("defaultProductUrl");
            } else if (j.has("productUrl")) {
                productUrl = j.getString("productUrl");
            }
            if (productUrl == null || productUrl.equals("")) { return null; }

            String imageUrl = "";
            if (j.has("defaultImageUrl")) {
                imageUrl = j.getString("defaultImageUrl");
            } else if (j.has("thumbnailImageUrl")) {
                imageUrl = j.getString("thumbnailImageUrl");
            }
            if (imageUrl == null || imageUrl.equals("")) { return null; }

            Product p = new Product(Long.parseLong(id), productName, productUrl, imageUrl);

            if (j.has("brandName")) {
                p.setBrandName(j.getString("brandName"));
            }

            if (j.has("brandId")) {
                p.setBrandId(j.getInt("brandId"));
            }

            if (j.has("gender")) {
                p.setGender(j.getString("gender"));
            }

            if (j.has("price")) {
                p.setPrice(j.getString("price"));
            }

            if (j.has("styles")) {
                JSONArray styles = j.getJSONArray("styles");
                for (int i = 0; i < styles.length(); i++) {
                    JSONObject s = styles.getJSONObject(i);
                    Style style = convertToStyle(s, Long.parseLong(id));
                    p.addStyle(style);
                }
            }

            return p;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static List<Product> convertToProducts(JSONArray jarray) {
        List<Product> results = new ArrayList<Product>();
        try {
            for (int i = 0; i < jarray.length(); i++) {
                JSONObject j = jarray.getJSONObject(i);
                Product prod = convertToProduct(j);
                results.add(prod);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return results;
    }

    private static Style convertToStyle(JSONObject s, long productId) {
        try {
            String styleId = s.getString("styleId");
            if (styleId == null || styleId.equals("")) { return null; }

            // send in the product id referencing the original item
            Style style = new Style(Long.parseLong(styleId), productId);

            if (s.has("price")) {
                style.setPrice(s.getString("price"));
            }

            if (s.has("imageUrl")) {
                style.setImageUrl(s.getString("imageUrl"));
            }

            if (s.has("productUrl")) {
                style.setProductUrl(s.getString("productUrl"));
            }

            return style;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }
}
